package com.gztms.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Zoey
 * @date 2023/4/18 21:04
 * @Description 正则校验
 **/
public class ValidateUtil {

    /**
     * 密码校验，长度在6~18之间，必须包含字母和数字，可以存在特殊字符
     * @param password 密码
     * @return
     */
    public static boolean checkPassword(String password){
        boolean flag;
        try{
            Pattern regex = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z\\\\W]{6,18}$");
            Matcher matcher = regex.matcher(password);
            flag = matcher.matches();
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    /**
     * 手机号码验证，11位数字，1开头，第二位数必须是3456789这些数字之一 *
     * @param mobileNumber
     * @return
     */
    public static boolean checkPhoneNumber(String mobileNumber) {
        boolean flag;
        try {
            Pattern regex = Pattern.compile("^1[3456789]\\d{9}$");
            Matcher matcher = regex.matcher(mobileNumber);
            flag = matcher.matches();
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }


    /**
     * 邮箱验证
     * @param email
     * @return
     */
    public static boolean checkEmail(String email)
    {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 判断给定字符串是否空白串。
     *  空白串是指由空格、制表符、回车符、换行符组成的字符串
     *      若输入字符串为null或空字符串，返回true
     * @param input 输入的信息
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }
}
